﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XYAC.Framework.WatchDog
{
    public static class WatchDogStarter
    {
        /// <summary>
        /// 尝试启动 WatchDog.exe 进程。
        /// </summary>
        /// <returns>如果成功启动 WatchDog，返回 true；否则，返回 false。</returns>
        public static bool StartWatchDog()
        {
            try
            {
                // 获取当前程序集的目录
                string assemblyPath = typeof(WatchDogStarter).Assembly.Location;
                string directory = Path.GetDirectoryName(assemblyPath);
                if (directory == null)
                {
                    return false;
                }

                // 构建 WatchDog.exe 的完整路径
                string watchDogExePath = Path.Combine(directory, "XYAC.Framework.WatchDog.exe");

                // 检查 WatchDog.exe 是否存在
                if (!File.Exists(watchDogExePath))
                {
                    return false;
                }

                // 启动 WatchDog.exe
                var startInfo = new ProcessStartInfo
                {
                    FileName = watchDogExePath,
                    UseShellExecute = true,
                    CreateNoWindow = true,
                };

                Process process = Process.Start(startInfo);

                // 确认进程是否成功启动
                return process != null && !process.HasExited;
            }
            catch
            {
                // 发生异常，返回 false
                return false;
            }
        }
        /// <summary>
        /// 尝试杀死 WatchDog.exe 进程，以使其退出工作。
        /// </summary>
        /// <returns>如果成功杀死 WatchDog 进程，返回 true；否则，返回 false。</returns>
        public static bool Exit()
        {
            try
            {
                // 获取当前程序集的目录
                string assemblyPath = typeof(WatchDogStarter).Assembly.Location;
                string directory = Path.GetDirectoryName(assemblyPath);
                if (directory == null)
                {
                    return false;
                }

                // 构建 WatchDog.exe 的完整路径
                string watchDogExePath = Path.Combine(directory, "XYAC.Framework.WatchDog.exe");

                // 检查 WatchDog.exe 是否存在
                if (!File.Exists(watchDogExePath))
                {
                    return false;
                }

                // 获取进程名（不带扩展名）
                string processName = Path.GetFileNameWithoutExtension(watchDogExePath);

                // 获取所有匹配的 WatchDog 进程
                var processes = Process.GetProcessesByName(processName);

                // 终止所有匹配的 WatchDog 进程
                foreach (var process in processes)
                {
                    process.Kill();
                    process.WaitForExit();
                }

                return true;
            }
            catch
            {
                // 发生异常，返回 false
                return false;
            }
        }
    }
}
